package com.lenovo.vod.player.setting.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;

import org.xmlpull.v1.XmlPullParserException;

public class HttpConnetcionUtils {

	public static String getConnection(String urlString,String methodName) throws XmlPullParserException, IOException ,SocketTimeoutException{
		URL url = new URL(urlString);
		HttpURLConnection mURLConnection =  (HttpURLConnection) url.openConnection();
		mURLConnection.setConnectTimeout(5000);
		mURLConnection.setReadTimeout(5000);
		if(mURLConnection.getResponseCode() !=200 && mURLConnection.getConnectTimeout()>5000){
			throw new IOException();
		}
		InputStream in = null;
		String resultCode = null;
		try {
			in = mURLConnection.getInputStream();
			resultCode = XmlParser.doParserXml(in);
		}finally{
			try {
				if(in != null){
					in.close();
				}
				if(mURLConnection != null){
					mURLConnection.disconnect();
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new IOException();
			}
		}
		if(resultCode != null && !"".equals(resultCode)){
			if("add".equalsIgnoreCase(methodName)){
				if("UserManagement.S_Ok".equalsIgnoreCase(resultCode)){
					return "OK";
				}else {
					return "NO";
				}
			}else if("delete".equalsIgnoreCase(methodName)){
				if("UserManagement.S_Ok".equalsIgnoreCase(resultCode)){
					return "OK";
				}else {
					return "NO";
				}
			}else if("get".equalsIgnoreCase(methodName)){
				if("UserManagement.S_Ok".equalsIgnoreCase(resultCode)){
					return "OK";
				}else {
					return "NO";
				}
			}
		}else{
			return null;
		}
		return null;
	}
	
}
